package com.android.monee.view.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Button;
import android.widget.DatePicker;

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

	private Button dateButton;
	private SimpleDateFormat sdf;
	
	public DatePickerFragment (Button dateButton, SimpleDateFormat sdf){
		this.dateButton = dateButton;
		this.sdf = sdf;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Date termDate = new Date();
		try {
			termDate = sdf.parse(dateButton.getText().toString());
		} catch (ParseException e) {}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(termDate);
		
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int mont = cal.get(Calendar.MONTH);
		int year = cal.get(Calendar.YEAR);
		
		return new DatePickerDialog(getActivity(), this, year, mont, day);
	}

	@Override
	public void onDateSet(DatePicker picker, int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		
		Date termDate = cal.getTime();
		dateButton.setText(sdf.format(termDate));
	}
	
}
